package com.example.demo.javatest.thread.pool;

import java.util.concurrent.CountDownLatch;

/**
 * Created by ljh on 2019/5/18.
 * 面试被问到c线程如何等待a,b 线程执行完毕在执行
 *
 * join 和 CountDownLatch：join 必须等线程内容全部执行完，而CountDownLatch则可以部分执行
 */
public class WaitThread {
    private static CountDownLatch latch = new CountDownLatch(2);

    public static void main(String[] args) throws Exception{
       fun2();
    }
    /**
     * 用CountDownLatch
     */
    public static void fun1() throws Exception{
        Thread a=new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    latch.countDown();
                }
                System.out.println("线程a启动");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("a第二阶段");
            }
        });
        a.start();
        Thread b=new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程b启动");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    latch.countDown();
                }
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("b第二阶段");
            }
        });
        b.start();
        latch.await();
        Thread c=new Thread(new Runnable() {
            @Override
            public void run() {

                System.out.println("线程c启动");
            }
        });
        c.start();
        //System.out.println("主线程结束");
    }

    /**
     * 用join
     */
    public  static void fun2() throws  Exception{
        Thread a=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程a启动");
            }
        });
        Thread b=new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程b启动");
                /*try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
            }
        });
        a.start();
        b.start();
        a.join();
        b.join();
        Thread c=new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程c启动");
            }
        });
        c.start();
    }
}
